//
//  HomeTableViewCell.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/24.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "HomeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "HomeWeiboFrameModel.h"
#import "HomeWeiboModel.h"
#import "UserModel.h"
#import "NSDateUtils.h"
#import "WeiboView.h"
#import "ThemeManager.h"

@implementation HomeTableViewCell

/**
 * 手动移除通知
 */
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangedNotification object:nil];
}


/**
 * 从xib中加载cell
 */
- (void)awakeFromNib {
    // Initialization code
    
    self.backgroundColor = [[ThemeManager defualtManager] getThemeColor:@"Theme_Main_color"];
    
    
    /**
     * 注册响应通知
     */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:kThemeDidChangedNotification object:nil];

    /**
     * 创建微博视图
     */
    self.weiboView = [[WeiboView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:self.weiboView];
        
}


/**
 * 根据通知切换cell背景颜色
 */
- (void)notificationAction:(NSNotification *)noti{
    self.backgroundColor = [[ThemeManager defualtManager] getThemeColor:@"Theme_Main_color"];
}



/**
 * 给frameModel赋值是调用
 */
- (void)setWeiboFrameModel:(HomeWeiboFrameModel *)weiboFrameModel{
    
    if (_weiboFrameModel != weiboFrameModel) {
        
        _weiboFrameModel = weiboFrameModel;
        
        /**
         * 获取frame中对应的model
         */
        HomeWeiboModel *weiboModel = self.weiboFrameModel.weiboModel;
        
        /**
         * 将各参数显示
         */
        self.createDate.text = [NSString stringWithFormat:@"时间:%@",[NSDateUtils weiboDateString:weiboModel.createDate]];         //通过工具类转化
        self.createDate.textColorName = @"Timeline_Time_color";     //使用主题颜色
        self.source.text = weiboModel.source;
        self.source.textColorName = @"Timeline_Time_color";
        
        self.repostsCount.text = [NSString stringWithFormat:@"转发:%d",[weiboModel.repostsCount intValue]];
        self.commentsCount.text = [NSString stringWithFormat:@"评论:%d",[weiboModel.commentsCount intValue]];
        self.attitudesCount.text = [NSString stringWithFormat:@"赞:%d",[weiboModel.attitudesCount intValue]];
        
        self.repostsCount.textColorName = @"Timeline_Name_color";
        self.commentsCount.textColorName = @"Timeline_Name_color";
        self.attitudesCount.textColorName =@"Timeline_Name_color";
        
        NSString *urlStr = weiboModel.userModel.profile_image_url;
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:urlStr]];
        
        self.userName.text = weiboModel.userModel.screen_name;
        
        
        /**
         * weiboViews设置
         */
        self.weiboView.weiboFrameModel = self.weiboFrameModel;
        
        self.weiboView.frame = self.weiboFrameModel.frame;
        
        [self setNeedsLayout];
    }
    
    
}


- (void)layoutSubviews{
    [super layoutSubviews];
}





/*
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
}
 */

@end
